package com.example.city_home_app.Activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.example.city_home_app.Bean.Down_MyFocus;
import com.example.city_home_app.Bean.Down_UserInfo;
import com.example.city_home_app.Fragment.Fragment_FansUser;
import com.example.city_home_app.Fragment.Fragment_FocusPost;
import com.example.city_home_app.Fragment.Fragment_FocusUser;
import com.example.city_home_app.Fragment.Fragment_userinfo_info;
import com.example.city_home_app.Fragment.Fragment_userinfo_moment;
import com.example.city_home_app.Fragment.Fragment_userinfo_post;
import com.example.city_home_app.Model.ApiModel;
import com.example.city_home_app.R;
import com.example.city_home_app.RetrofitListener;
import com.example.city_home_app.UI.UIUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyFocusActivity extends BaseActivity {
    @BindView(R.id.toolbar_mylist)
    Toolbar toolbarMylist;
    @BindView(R.id.tv_mylist_focus)
    TextView tvMylistFocus;
    @BindView(R.id.tv_mylist_ffocus)
    TextView tvMylistFfocus;
    @BindView(R.id.tv_mylist_focuspost)
    TextView tvMylistFocuspost;
    @BindView(R.id.myfocus_vp_content)
    FrameLayout myfocusVpContent;

    private Down_MyFocus down_myFocus;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private Fragment fragment_focus, fragment_fans, fragment_post;
    private int focus_count,fans_count;
    private ApiModel model = new ApiModel();

    public Down_MyFocus getDown_myFocus() {
        return down_myFocus;
    }

    private RetrofitListener<Down_MyFocus> listener = new RetrofitListener<Down_MyFocus>() {
        @Override
        public void onResponse(Down_MyFocus listener) {
            down_myFocus = listener;
            focus_count = listener.getFocus().size();
            fans_count = listener.getFans().size();
            updatefocuscount();
        }

        @Override
        public void onFail(String msg) {

        }
    };

    public void updatedata(){
        model.getmyfocuslist(BaseActivity.getUserinfo().getUserid(),listener);
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_myfocuslist;
    }

    @Override
    protected void initViews() {
        super.initViews();
        ButterKnife.bind(this);
        showToast();

        Window window = MyFocusActivity.this.getWindow();
        //如果系统5.0以上
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(0xff49afcd);
        }

    }

    @Override
    protected void initData() {
        super.initData();
        updatedata();
        selectPage(0);
    }

    private void selectPage(int select) {
        resetView();
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        hideFragment(fragmentTransaction);
        switch (select) {
            case 0:
                if (fragment_focus == null) {
                    fragment_focus = new Fragment_FocusUser();
                    fragmentTransaction.add(R.id.myfocus_vp_content, fragment_focus);
                } else {
                    fragmentTransaction.show(fragment_focus);
                }
                tvMylistFocus.setTextColor(0xff49afcd);
                break;
            case 1:
                if (fragment_fans == null) {
                    fragment_fans = new Fragment_FansUser();
                    fragmentTransaction.add(R.id.myfocus_vp_content, fragment_fans);
                } else {
                    fragmentTransaction.show(fragment_fans);
                }
                tvMylistFfocus.setTextColor(0xff49afcd);
                break;
            case 2:
                if (fragment_post == null) {
                    fragment_post = new Fragment_FocusPost();
                    fragmentTransaction.add(R.id.myfocus_vp_content, fragment_post);
                } else {
                    fragmentTransaction.show(fragment_post);
                }
                tvMylistFocuspost.setTextColor(0xff49afcd);
                break;
        }
        fragmentTransaction.commit();
    }

    private void hideFragment(FragmentTransaction fragmentTransaction) {
        if (fragment_focus != null) {
            fragmentTransaction.hide(fragment_focus);
        }
        if (fragment_fans != null) {
            fragmentTransaction.hide(fragment_fans);
        }
        if (fragment_post != null) {
            fragmentTransaction.hide(fragment_post);
        }
    }

    private void resetView() {
        tvMylistFocus.setTextColor(0xff929292);
        tvMylistFfocus.setTextColor(0xff929292);
        tvMylistFocuspost.setTextColor(0xff929292);
    }

    private void updatefocuscount() {
        tvMylistFocus.setText("关注用户("+focus_count+")");
        tvMylistFfocus.setText("粉丝("+fans_count+")");
    }



    @OnClick({R.id.tv_mylist_focus, R.id.tv_mylist_ffocus, R.id.tv_mylist_focuspost})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_mylist_focus:
                selectPage(0);
                break;
            case R.id.tv_mylist_ffocus:
                selectPage(1);
                break;
            case R.id.tv_mylist_focuspost:
                selectPage(2);
                break;
        }
    }

    private void showToast() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        toolbarMylist = (Toolbar) findViewById(R.id.toolbar_mylist);
        setSupportActionBar(toolbarMylist);
        toolbarMylist.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
}
